package com.yan_jiu_sheng.LeetCodeHot100.AC;


/**
 * https://leetcode.cn/problems/search-insert-position/description/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 *
 * @author yulongTian
 * @create 2024-07-08 14:45
 */
public class Test61 {
    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 6, 7, 8, 9, 12, 18};
        int target = 10;
        System.out.println(new Test61().searchInsert(nums, target));
    }

    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
//        System.out.println(left);
//        System.out.println(right);
//        System.out.println(left + (right - left) / 2);
        return left + (right - left) / 2;
    }
}
